<?php

namespace app\controller\admin\info;


use app\BaseController;
use app\common\repositories\info\AdPackageRepository;
use app\common\repositories\info\AdTaskRepository as repository;
use think\App;
use think\exception\ValidateException;

class AdTask extends BaseController
{
    protected $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }


    public function lst()
    {
        [$page,$limit] = $this->request->page();
        $where = $this->request->params([
            'title','status',
        ]);
        return success($this->repository->getAdminLst($where,$page,$limit));
    }


    public function audit()
    {
        $id = input('task_id','');
        $task = $this->repository->get($id);

        if($task->status != 1){
            throw new ValidateException('状态有误');
        }

        $status = input('status',1) == 1 ? 2 : -1;

        if($status == -1){
            $this->repository->refund($task->order_sn);
        }else{
            $task->save(['status'=>$status]);
        }

        return success('审核成功');
    }





    public function packageCreate()
    {
        /**@var $make AdPackageRepository**/
        $make = app()->make(AdPackageRepository::class);
        $data = $this->checkParams();
        $make->create($data);
        return success('创建成功');
    }

    public function packageUpdate($id)
    {
        /**@var $make AdPackageRepository**/
        $make = app()->make(AdPackageRepository::class);
        $data = $this->checkParams();
        $package = $make->get($id);
        if($package){
            $package->save($data);
        }
        return success('编辑成功');
    }

    public function checkParams()
    {
        $params = $this->request->params([
          'title','price','count','sd_status'
        ]);

        $this->validate($params,[
            'title|套餐标题'=>'require',
            'price|套餐价格'=>'require',
            'count|人数'=>'require|number',
            'sd_status|首单状态'=>'require',
        ]);

        return $params;
    }

    public function packageLst()
    {
        /**@var $make AdPackageRepository**/
        $make = app()->make(AdPackageRepository::class);
        $list = $make->getSearch()->select();
        return success($list);
    }



    public function packageDelete($id)
    {
        /**@var $make AdPackageRepository**/
        $make = app()->make(AdPackageRepository::class);
        $package = $make->get($id);
        if($package){
            $package->delete();
        }
        return success('删除成功');
    }



}